<?php
include_once $_SERVER['DOCUMENT_ROOT']."/controller/connect.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/Output.php";
include_once $_SERVER['DOCUMENT_ROOT']."/model/NumericalOutput.php";
/**
 * Created by PhpStorm.
 * User: lwh
 * Date: 2017/7/21
 * Time: 15:33
 */
class NumericalOutputDAO{
    const FIELD_ID = "outputId";
    const FIELD_ACTION_ID = "actionId";
    const FIELD_DESCRIPTION = "outputDesc";
    const FIELD_VALUE = "outputValue";
    const FIELD_WORKER_ID = "workerId";
    const FIELD_INDICATOR = "outputIndicator";
    const FIELD_INTERVAL = "intervalIndicator";
    const FIELD_UPPER_BOUND = "upBound";
    const FIELD_LOWER_BOUND = "lowBound";
    const FIELD_AGGREGATION_METHOD = "numberAggregate";

    public $conn;

    function __construct()
    {
        $this->conn = new db();
    }

    //插入数字输出
    public function insert($number){
        $db = $this->conn->getDBConn();
        $stmt = $db->prepare("insert into numericaloutput(actionId,outputDesc,outputValue,workerId,outputIndicator,intervalIndicator,upBound,lowBound,numberAggregate) values(?,?,?,?,?,?,?,?,?)");
        $stmt->bindParam(1,$number[self::FIELD_ACTION_ID],PDO::PARAM_INT);
        $stmt->bindParam(2,mb_convert_encoding($number[self::FIELD_DESCRIPTION],'GBK','UTF-8'),PDO::PARAM_STR);
        $stmt->bindParam(3,mb_convert_encoding($number[self::FIELD_VALUE],'GBK','UTF-8'),PDO::PARAM_STR);
        $stmt->bindParam(4,$number[self::FIELD_WORKER_ID],PDO::PARAM_INT);
        $stmt->bindParam(5,$number[self::FIELD_INDICATOR],PDO::PARAM_INT);
        $stmt->bindParam(6,$number[self::FIELD_INTERVAL],PDO::PARAM_INT);
        $stmt->bindParam(7,$number[self::FIELD_UPPER_BOUND]);
        $stmt->bindParam(8,$number[self::FIELD_LOWER_BOUND]);
        $stmt->bindParam(9,$number[self::FIELD_AGGREGATION_METHOD],PDO::PARAM_INT);
        $result = $stmt->execute();
        return $result;
    }

    //删除数字输出
    public function delete($numberId){

    }

    //查询数字输出
    public function query($numberId){
        $sql = "select * from numericaloutput where ".self::FIELD_ID." = ".$numberId;
        $db = $this->conn->getDBConn();
        $row = $db->query($sql)->fetch(PDO::FETCH_ASSOC);//调用查询函数query(),并以关联数组的形式储存
        if($row){
            $number = $this->loadDataFromResult($row);
            return $number;
        }
        else
            return null;
    }

    //更新数字输出
    public function update($number){

    }

    //返回一个action对应的所有数字输出
    public function queryOutputListByAction($actionId){
        $sql = "select * from numericaloutput where ".self::FIELD_ACTION_ID." = ".$actionId;
        $db = $this->conn->getDBConn();
        $rows = $db->query($sql)->fetchAll(PDO::FETCH_ASSOC);//调用查询函数query(),并以关联数组的形式储存
        if(count($rows) > 0){
            $numbers = array();
            foreach ($rows as $row){
                $number = $this->loadDataFromResult($row);
                array_push($numbers,$number);
            }
            return $numbers;
        }
        else
            return null;
    }

    //根据indicator返回一个action对应的所有数字输出
    public function queryOutputListByActionAndIndicator($actionId,$indicator){
        $sql = "select * from numericaloutput where ".self::FIELD_ACTION_ID." = ".$actionId." and ".self::FIELD_INDICATOR." = ".$indicator;
        $db = $this->conn->getDBConn();
        $rows = $db->query($sql)->fetchAll(PDO::FETCH_ASSOC);//调用查询函数query(),并以关联数组的形式储存
        if(count($rows) > 0){
            $numbers = array();
            foreach ($rows as $row){
                $number = $this->loadDataFromResult($row);
                array_push($numbers,$number);
            }
            return $numbers;
        }
        else
            return null;
    }

    //返回用户针对某个action的所有输出
    public function queryOutputListByActionAndUser($actionId,$userId,$indicator){
        $sql = "select * from numericaloutput where ".self::FIELD_ACTION_ID." = ".$actionId." and ".self::FIELD_INDICATOR." = ".$indicator." and ".self::FIELD_WORKER_ID." = ".$userId;
        $db = $this->conn->getDBConn();
        $rows = $db->query($sql)->fetchAll(PDO::FETCH_ASSOC);//调用查询函数query(),并以关联数组的形式储存
        if(count($rows) > 0){
            $numbers = array();
            foreach ($rows as $row){
                $number = $this->loadDataFromResult($row);
                array_push($numbers,$number);
            }
            return $numbers;
        }
        else
            return null;
    }

    //从结果集中构建出pictureoutput对象
    private function loadDataFromResult($row){
        $number = new NumericalOutput();
        $number->setId($row[self::FIELD_ID]);
        $number->setActionId($row[self::FIELD_ACTION_ID]);
        $number->setDesc($row[self::FIELD_DESCRIPTION]);
        $number->setValue($row[self::FIELD_VALUE]);
        $number->setWorkerId($row[self::FIELD_WORKER_ID]);
        $number->setIndicator($row[self::FIELD_INDICATOR]);
        $number->setInterval($row[self::FIELD_INTERVAL]);
        $number->setLowerBound($row[self::FIELD_LOWER_BOUND]);
        $number->setUpperBound($row[self::FIELD_UPPER_BOUND]);
        $number->setAggregationMethod($row[self::FIELD_AGGREGATION_METHOD]);
        return $number;
    }
}
